<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;

use App\Models\Admins;
use App\Models\Containers;
use App\Models\Packages;
use App\Models\Staffs;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class AdminController extends Controller
{

    public function index(){
        return view('index.login');
    }

    public function login(Request $request)
    {
        $request->validate([
            'admin_name' => 'required|string|min:4|max:20',
            'password' => 'required|string|min:4|max:20',
        ]);


        $user = Admins::where('username', $request->username)
            ->where('password', $request->password)
            ->first();

        if (!$user) {
            \Log::warning('用户不存在或密码错误', ['username' => $request->username]);
            return back()->withErrors(['username' => '用户名或密码错误']);
        }



        return redirect()->intended(route('admin.dashboard'));
    }

    public function dashboard(){

        $staffs = Staffs::orderBy('id', 'asc')
            ->whereNotNull('route_id')
            ->get();

        $groupedStaffs = $staffs->groupBy(['route_id', 'campus_id']);
        return view('index.dashboard', compact('groupedStaffs'));




    }

    public function Package_List(){

        $packages = Packages::with([
            'packages_info',
            'from_campus',
            'to_campus',
            'sender',
            'delivery'
        ])->paginate(10);
        return view('index.Package_List', compact('packages'));
    }

    public function Package_Detail($Package_Id){
        $package = Packages::with([
            'packages_info',
            'from_campus',
            'to_campus',
            'sender',
            'delivery',
            'dispatch'
        ])->where('id',$Package_Id)->first();
        return view('index.Package_Detail', compact('package'));
    }

    public function Courier_List(){
        $packages = Staffs::with([
        'camspus'
        ])->whereNull('route_id')->paginate(10);

        return view('index.Courier_List',compact('packages'));
    }

    public function Password_Reset($staffId)
    {

        $staff = Staffs::findOrFail($staffId);


        return view('admin.password_reset', compact('staff'));
    }

    public function Update_Password(Request $request, $staffId)
    {

        $validatedData = $request->validate([
            'password' => 'required|string|min:8|max:20|confirmed',
        ]);


        $staff = Staffs::findOrFail($staffId);


        $staff->update([
            'password' => Hash::make($validatedData['password'])
        ]);


        return redirect()->route('admin.Password_Reset_Successful')
            ->with('success', '密码已成功重置');
    }

    public function Create_New_Courier()
    {
        $campuses = \App\Models\Campus::all(); // 获取校区列表
        return view('index.Create_New_Courier', compact('campuses'));
    }

    public function store(Request $request)
    {
        $validatedData = $request->validate([
            'first_name' => 'required|string|max:255',
            'last_name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:staffs,email',
            'phone' => 'required|string|min:8|max:12|unique:staffs,phone',
            'plate_number' => 'required|string|min:4|max:12|unique:staffs,plate_number',
            'password' => 'required|string|min:4|max:20|confirmed',
            'route_id' => 'required|exists:routes,id',
            'campus_id' => 'required|exists:campuses,id'
        ], [
            // Custom Chinese error messages
            'first_name.required' => '名字为必填项',
            'last_name.required' => '姓氏为必填项',
            'email.required' => '邮箱为必填项',
            'email.email' => '请输入有效的邮箱地址',
            'email.unique' => '该邮箱已被注册',
            'phone.required' => '电话号码为必填项',
            'phone.min' => '电话号码至少需要8个字符',
            'phone.max' => '电话号码不能超过12个字符',
            'phone.unique' => '该电话号码已被使用',
            'plate_number.required' => '车牌号为必填项',
            'plate_number.min' => '车牌号至少需要4个字符',
            'plate_number.max' => '车牌号不能超过12个字符',
            'plate_number.unique' => '该车牌号已被使用',
            'password.required' => '密码为必填项',
            'password.min' => '密码至少需要4个字符',
            'password.max' => '密码不能超过20个字符',
            'password.confirmed' => '两次输入的密码不匹配',
            'route_id.required' => '请选择关联路线',
            'route_id.exists' => '选择的路线无效',
            'campus_id.required' => '请选择校区',
            'campus_id.exists' => '选择的校区无效'
        ]);

        Staffs::create([ // 使用正确的模型
            'first_name' => $validatedData['first_name'],
            'last_name' => $validatedData['last_name'],
            'phone' => $validatedData['phone'],
            'email' => $validatedData['email'],
            'password' => Hash::make($validatedData['password']),
            'campus_id' => $validatedData['campus_id']
        ]);

        return redirect()->route('admin.Create_New_Courier')->with('success', '快递员创建成功!');
    }

    public function Create_New_Trucker(){
        $campuses = \App\Models\Campus::all(); // 获取校区列表
        return view('index.Create_New_Trucker',compact('campuses'));
    }

    public function store_Trucker(Request $request){
        $validatedData = $request->validate([
            'first_name' => 'required|string|max:255',
            'last_name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:staffs,email',
            'plate_number' => 'required|string|min:4|max:12|unique:staffs,plate_number',
            'password' => 'required|string|min:4|max:20|confirmed',
            'route_id' => 'required|exists:routes,id'
        ], [
            // Custom error messages
            'first_name.required' => '请填写名字',
            'last_name.required' => '请填写姓氏',
            'email.required' => '请填写邮箱地址',
            'email.unique' => '该邮箱已被注册',
            'plate_number.required' => '请填写车牌号',
            'plate_number.unique' => '该车牌号已被使用',
            'password.confirmed' => '两次输入的密码不匹配',
            'route_id.required' => '请选择关联路线'
        ]);

        Staffs::create([ // 使用正确的模型
            'first_name' => $validatedData['first_name'],
            'last_name' => $validatedData['last_name'],
            'plate_number' => $validatedData['plate_number'],
            'email' => $validatedData['email'],
            'password' => Hash::make($validatedData['password']),
            'route_id' => $validatedData['route_id']
        ]);

        return redirect()->route('admin.Create_New_Trucker')->with('success', '司机创建成功!');
    }




    public function Campus_List(){
        $packages = Containers::with([
            'from_campus',
            'to_campus'

        ])->get();

        return view('index.Campus_List', compact('packages'));
    }

    public function Trucker_List(){
        $packages = Staffs::with([
            'camspus',
            'route'
        ])->whereNotNull('route_id')->paginate(10);


        return view('index.Trucker_List',compact('packages'));
    }

    public function Password_Reset_Successful(){


        return view('index.Password_Reset_Successful');
    }

    public function Password_Reset_Trucker($Package_Id){
        $package = Staffs::with([
            'route',
            'camspus'

        ])->where('id',$Package_Id)->first();

        return view('index.Password_Reset_Trucker',compact('package'));
    }
    public function Password_Reset_Successful_Trucker(){
        return view('index.Password_Reset_Successful_Trucker');
    }
}
